import cv2
import imutils

haar_upper_body_cascade = cv2.CascadeClassifier("data/haarcascade_mcs_upperbody.xml")
# haar_upper_body_cascade = cv2.CascadeClassifier("data/haarcascade_frontalface_alt2.xml")

# Uncomment this for real-time webcam detection
# If you have more than one webcam & your 1st/original webcam is occupied,
# you may increase the parameter to 1 or respectively to detect with other webcams, depending on which one you wanna use.

# video_capture = cv2.VideoCapture(0)

# For real-time sample video detection
video_capture = cv2.VideoCapture('rtmp://10.200.9.104/rtp/0BEBDC03')
video_width = video_capture.get(3)
video_height = video_capture.get(4)

count = 0
upper_body = []
# x, y, h, w
guard = [250, 200, 500, 500]

while True:
    ret, frame = video_capture.read()

    frame = imutils.resize(frame, width=1000)  # resize original video for better viewing performance


    if count % 10 == 0:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # convert video to grayscale
        upper_body = haar_upper_body_cascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(50, 100),  # Min size for valid detection, changes according to video size or body size in the video.
            flags=cv2.CASCADE_SCALE_IMAGE
        )

    count = count + 1
    print(count)
    # Draw a rectangle around the upper bodies
    for (x, y, w, h) in upper_body:
        # print(upper_body)
        # creates green color rectangle with a thickness size of 1
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 1)
        # creates green color text with text size of 0.5 & thickness size of 2
        cv2.putText(frame, "upper body", (x + 5, y + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

    cv2.rectangle(frame, (guard[0], guard[1]), (guard[0] + guard[3], guard[1] + guard[2]), (0, 0, 255), 2)
    cv2.putText(frame, "guard range", (guard[0] + 5, guard[1] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)

    cv2.imshow('Video', frame)  # Display video

    # stop script when "q" key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release capture
video_capture.release()
cv2.destroyAllWindows()
